package com.kzd.btgame.main.money

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.ActivityMoneyTaskPlayBinding
import com.kzd.btgame.jetpack.vm.MoneyViewModel
import com.kzd.btgame.main.ProblemDetailsAction
import com.kzd.btgame.main.money.adapter.MoneyTaskPlayAdapter
import com.kzd.btgame.main.toProblemDetailsActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MoneyTaskPlayActivity : OwnerViewBindingActivity<ActivityMoneyTaskPlayBinding>() {

    private val mMoneyTaskPlayAdapter = MoneyTaskPlayAdapter()

    private val mViewModel: MoneyViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMoneyTaskPlayBinding
        get() = ActivityMoneyTaskPlayBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.tlMoneyTaskPlayBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.tlMoneyTaskPlayBar.setOnClickRightTextView {
            toProblemDetailsActivity(ProblemDetailsAction.PLAY)
        }

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMoneyTaskPlayAdapter

        mMoneyTaskPlayAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mMoneyTaskPlayAdapter.getItem(position)
            if (view.id == R.id.tvMoneyTaskPlayGo) {
                if (itemData.status == 0 || itemData.status == 2) {
                    mViewModel.moneyTaskPlayGet(itemData.id)
                }
            }
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.moneyTaskPlay()
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.moneyTaskPlay(isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.moneyTaskPlay()
    }

    override fun initViewMode() {
        mViewModel.moneyTaskPlayLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mMoneyTaskPlayAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMoneyTaskPlayAdapter.addData(data)
            } else {
                mMoneyTaskPlayAdapter.setNewInstance(data)
            }
        })

        mViewModel.moneyTaskPlayGetLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            mViewModel.moneyTaskPlay()
        })
    }
}